package com.google.android.daca;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.daca.types.Participant;
import com.google.android.daca.types.Poll;
import com.google.android.daca.util.OptionAggregate;
import com.google.android.daca.util.PollResultsUtil;

public class PollResultsDetailsActivity extends ExpandableListActivity {
	ExpandableListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getIntent().getExtras();

		if (bundle != null) {
			// Set up our adapter
			Poll parceledPoll = bundle.getParcelable(getResources().getString(R.string.extra_poll_object));
			setTitle(parceledPoll.getTitle() + " Results");
			mAdapter = new MyExpandableListAdapter(parceledPoll);
			setListAdapter(mAdapter);
		}
		
	}

	public class MyExpandableListAdapter extends BaseExpandableListAdapter {
		// Sample data set.  children[i] contains the children (String[]) for groups[i].
		private List<OptionAggregate> options;
		private Poll poll;
		
		MyExpandableListAdapter(Poll poll) {
			this.poll = poll;
			this.options = PollResultsUtil.getOptionAggregates(this.poll);
			Collections.sort(options);
		}
		public Object getChild(int groupPosition, int childPosition) {
			return poll.getParticipants().get(childPosition);
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public int getChildrenCount(int groupPosition) {
			return poll.getParticipants().size();
		}

		public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
				View convertView, ViewGroup parent) {
			LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.pollresult_detail, null);
			TextView pollDetailOption = (TextView) view.findViewById(R.id.poll_result_detail_participant);
			Participant participant = (Participant) getChild(groupPosition, childPosition);
			pollDetailOption.setText(participant.getName());
			int vote = participant.getPreferences().get(groupPosition);
			ImageView voteImg = (ImageView) view.findViewById(R.id.poll_result_detail_vote_image);
			if (vote == 1) {
				voteImg.setImageResource(R.drawable.checkmarkgreen);
			} else {
				voteImg.setImageResource(R.drawable.xmark2_red);
			}
			return view;
		}

		public Object getGroup(int groupPosition) {
			Iterator<OptionAggregate> iterator = options.iterator();
			OptionAggregate result = null;
			while (iterator.hasNext() && groupPosition >= 0) {
				result = iterator.next();
				--groupPosition;
			}
			return result;
		}

		public int getGroupCount() {
			return options.size();
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
				ViewGroup parent) {
			LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.pollresult, null);
			TextView pollResultOption = (TextView) view.findViewById(R.id.poll_result_option_text);
			TextView voteYes = (TextView) view.findViewById(R.id.poll_result_vote_yes);
			TextView voteNo = (TextView) view.findViewById(R.id.poll_result_vote_no);
			OptionAggregate option = (OptionAggregate) getGroup(groupPosition);
			pollResultOption.setText(option.getValue());
			voteYes.setText(String.valueOf(option.getYesCount()));
			voteNo.setText(String.valueOf(option.getNoCount()));
			return view;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public boolean hasStableIds() {
			return true;
		}
	}
}
